//
//  XJYMyViewController.m
//  ShopMall
//
//  Created by XJY on 17/1/17.
//  Copyright © 2017年 XinJinYing. All rights reserved.
//

#import "XJYMyViewController.h"
#import "XJYLandingViewController.h"
#import "XJYMyTableView.h"
#import "XJYMyHeadView.h"
#import "XJYLoginViewController.h"
@interface XJYMyViewController ()

//头部视图
@property (nonatomic, strong) XJYMyHeadView *headView;

//tableview
@property (nonatomic, strong) XJYMyTableView *myTableView;


@end

@implementation XJYMyViewController

#pragma mark - 懒加载
- (XJYMyHeadView *)headView
{
    if (!_headView) {
        _headView = [[XJYMyHeadView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 160)];
        __weak typeof(self) weakSelf = self;
        _headView.landingBlock = ^(){
            XJYLandingViewController *landingVC = [[XJYLandingViewController alloc] init];
            [weakSelf.navigationController pushViewController:landingVC animated:YES];
        };
        
        _headView.loginBlock = ^(){
            XJYLoginViewController *loginVC = [[XJYLoginViewController alloc] init];
            [weakSelf.navigationController pushViewController:loginVC animated:YES];
        };
    }
    return _headView;
}

- (XJYMyTableView *)myTableView
{
    if (!_myTableView) {
        _myTableView = [[XJYMyTableView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStylePlain];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.tableHeaderView = self.headView;
        __weak typeof(self) weakSelf = self;
        _myTableView.exitLoginBlock = ^() {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"ISLOGIN"];
            [weakSelf.myTableView reloadData];
            [weakSelf.headView reloadHeadView];
        };
       
    }
    return _myTableView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.myTableView reloadData];
    [self.headView reloadHeadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = MainColor;
    
    //让头部视图的顶部从导航栏的底部开始，否则会被导航栏盖到
    self.edgesForExtendedLayout = 0;
    
    [self.view addSubview:self.headView];
    [self.view addSubview:self.myTableView];
    
   // [self setupLayout];
}

//- (void)setupLayout
//{
//    __weak typeof(self) weakSelf = self;
//    
//    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.left.top.equalTo(weakSelf.view);
//        make.height.equalTo(@160);
//    }];
//    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(weakSelf.view);
//        make.top.equalTo(weakSelf.headView.mas_bottom).offset(30);
//        make.height.equalTo(@300);
//    }];
//    
//}



@end
